
import requests
from time import sleep
from lxml import  etree
import csv

if __name__ == '__main__':
    # i = int(0)
    headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)'
                  ' Chrome/116.0.0.0 Safari/537.36 Edg/116.0.1938.76'
    }

    url = 'https://bj.58.com/?refrom=m58'
    page_text = requests.get(url=url, headers=headers).text
    tree = etree.HTML(page_text)
    a_lists = tree.xpath('/html/body/div[3]/div[1]/div[1]/div/div[2]/div[1]')
    #/html/body/div[3]/div[1]/div[1]/div/div[2]/div[1]/a[1]

    a_titles = tree.xpath('/html/body/div[3]/div[1]/div[1]/div/div[2]/div[1]/a/text()')
    a_srcs = tree.xpath('/html/body/div[3]/div[1]/div[1]/div/div[2]/div[1]/a/@href')
    # print(a_titles,a_srcs)
    name_lists = []
    for a_src in a_srcs:
        name_srcs = 'https://bj.58.com'+a_src+'pn/'
        #一个全是网址的列表
        name_lists.append(name_srcs)
    with open('./物品和网址11.csv','w',newline='',encoding='utf-8') as csvfile:
        writer = csv.writer(csvfile)
        # 写入列标题
        # writer.writerow(['物品名称', '网址'])
        # 这里我们使用zip函数将两个列表配对，然后写入CSV文件
        for row in zip(a_titles, name_lists):
            writer.writerow(row)
#
#     with open('物品和网址.csv','r',encoding='utf-8') as csvfile:
#         reader = csv.reader(csvfile)
#         # row一个含有物品和网址的对应列表，只含有一组值。reader中含有多个小列表。
#         for row in reader:
#
#
#
#
# def get_data(url):
#     for j in range(1,5):
#         data_src = list +'j'
#         page_text_data = requests.get(url= data_src,headers=headers).text
#         tree = etree.HTML(page_text_data)
#         data_text = tree.xpath('//div[@class = "t info"]/text()')
#         data_text




